package org.myhomework.dao;

import org.myhomework.entity.User;
import org.myhomework.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public final class UserDao {

    public static User getByUserName(String username) {
        try (Connection connection = JdbcUtil.getConnection()) {
            PreparedStatement preparedStatement = connection.prepareStatement("select id, username, password, name, type from user where username = ? limit 1");
            preparedStatement.setString(1, username);
            ResultSet resultSet = preparedStatement.executeQuery();
            User user = null;
            while (resultSet.next()) {
                user = User.mapResult(resultSet);
                break;
            }
            resultSet.close();
            return user;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static User getById(Integer id) {
        try (Connection connection = JdbcUtil.getConnection()) {
            PreparedStatement preparedStatement = connection.prepareStatement("select id, username, password, name, type from user where id = ? limit 1");
            preparedStatement.setInt(1, id);
            ResultSet resultSet = preparedStatement.executeQuery();
            User user = null;
            while (resultSet.next()) {
                user = User.mapResult(resultSet);
                break;
            }
            resultSet.close();
            return user;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void saveUser(User user) {
        try (Connection connection = JdbcUtil.getConnection()) {
            PreparedStatement preparedStatement = connection.prepareStatement("insert into user(username, password, name, type) values (?, ?, ?, ?)", PreparedStatement.RETURN_GENERATED_KEYS);
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getName());
            preparedStatement.setInt(4, user.getType());
            preparedStatement.executeUpdate();
            ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
            generatedKeys.next();
            user.setId(generatedKeys.getInt(1));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean changePwd(Integer id, String password) {
        try (Connection connection = JdbcUtil.getConnection()) {
            PreparedStatement preparedStatement = connection.prepareStatement("update user set password = ? where id = ?");
            preparedStatement.setString(1, password);
            preparedStatement.setInt(2, id);
            return preparedStatement.executeUpdate() > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
